# -*- coding: utf-8 -*-
"""
Created on Thu May 18 13:37:12 2017

@author: XUGUOQIANG371
"""
import pandas as pd
import numpy as np
from scipy.stats import ks_2samp as KS
from sklearn.linear_model import LogisticRegression
from scipy.stats import spearmanr,pearsonr
import os
def KSmetrics(ytest,yprob):
    prob1 = yprob[ytest==1,0]
    prob2 = yprob[ytest==0,0]
    ksvalue = KS(prob1,prob2)
    return ksvalue

def computeRankCorrelation(trainData,label):
    corr = []
    pcorr = []
    for x in trainData.T:    
        corr.append(spearmanr(x,label))  
        pcorr.append(pearsonr(x,label))
    return corr,pcorr
    
class Model(object):
    def __init__(self):
        self.fortuneIdno=[]
        self.fortuneLabel=[]
        self.xtrain=[]
        
    def loadFortuneData(self,path):
        df = pd.read_csv(path,header=0,sep=',')
        df = df.groupby(['cust_lvl_2.idno'],as_index=False).max()
        ydata = df.values
        # 获取非缺失的idno 
        idno_s = ydata[:,0].astype(str)
        label = ydata[:,2].astype(str)
        index = label!='nan'
        idno_s = idno_s[index]
        # 对label进行encoding：M0-M2，label=0；M3-M5，label=1
        label = label[index].astype(float)
        label[label<=2]=0.0
        label[label>=2]=1.0
        self.fortuneLabel = label
        self.fortuneIdno = idno_s
    
    def loadFeatures(self,path,idno_s):
        df = pd.read_csv(path,header=0,sep=',')
        data = df.values.astype(str)
        # 找到idno_t中的idno_s
        idno_t = data[:,0]
        trainData = np.zeros((259,8))
        count = 0
        for x in idno_s:
            trainData[count,:] = data[idno_t==x.lower(),1:]
            count += 1
        self.xtrain = trainData
    
    def fit(self,xtrain,fortuneLabel):
        clf = LogisticRegression(C=0.1)
        clf.fit(xtrain,fortuneLabel)
        yprob = clf.predict_proba(xtrain)
        print(KSmetrics(fortuneLabel,yprob))
        CM,PCM = computeRankCorrelation(xtrain,fortuneLabel)
        

if __name__=='__main__':
    model = Model()
    model.loadFortuneData('cust_lvl_2.csv')
    model.loadFeatures('geo_params_md5.csv',model.fortuneIdno)
    model.fit(model.xtrain,model.fortuneLabel)